
/**
 * 七牛云上传
 * @param uploadsurl 上传路径
 * @param uploadstype 上传类型
 * @param file 文件
 * @param token 七牛云token
 * @returns
 */
import * as qiniu from "qiniu-js";
const loading = (title: any, time: number) =>
{
    if (time > 0)
    {
        uni.showLoading({
            title: title,
        });
    } else
    {
        uni.hideLoading();
    }
};
export const qiNiuUpload = (uploadsurl: string, uploadstype: string, file: any, token: string) =>
{
    loading('0%', 9999);
    return new Promise((resolve, reject) =>
    {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();

        const config = {
            useCdnDomain: true, //表示是否使用 cdn 加速域名，为布尔值，true 表示使用，默认为 false。
            region: qiniu.region.z2, // 根据地区不同，官网有不同的配置
            concurrentRequestLimit: 1, // 并发请求个数
        };
        // const key = "uploads/images/" + year + month + day + "/" + new Date().getTime() + ".png";
        const key = uploadsurl + year + month + day + "/" + new Date().getTime() + uploadstype;
        const observable = qiniu.upload(file, key, token, config);
        const observer = {
            next(res: any)
            {
                const percent = Math.floor(res.total.percent) + "%"; // 转换成百分比
                loading(percent, 9999);
            },
            error(err: any)
            {
                reject(err);
            },
            complete(res: any)
            {
                loading(100, -1);
                resolve(res);
            }
        };
        const response = observable.subscribe(observer);
    })
}